<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */


class Action extends admin_abs
{
	function index()
	{
		$id = _GET('id', 0);
		if (get_item_menu_type($id)) {
			redirect('?url=item&cid='.$id);
		} else {
			$data = _model('category')->getList("WHERE parent_id=$id");
			Smarty3::instance()->assign('data', $data);
			Smarty3::instance()->display('h_dev_html/category_list.html');
		}

	}
	
	function create()
	{
		$pid = _GET('id', 0);
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$title = _POST('title');
			$view_order = _POST('view_order', 0);
			$is_hide = _POST('is_hide', 0);
			$img = _POST('img');
			if (!get_category_type($pid)) {
				msg('当前的专辑下有照片不能创建子专辑');
			}
			if (!$title) {
				msg('名称必须添写');
			}
			$insert = array('title'=>$title, 'view_order'=>$view_order, 'is_hide'=>$is_hide);
			if ($pid) {
				$insert['parent_id'] = $pid;
			}
			if ($img) {
				$insert['img'] = $img;
			}
			_model('category')->create($insert);
			redirect('?url=category&id='.$pid);
        } else {
        	Smarty3::instance()->display('h_dev_html/category_create.html');
        }
	}
	
	function update()
	{
		$id = _GET('id', 0);
		if (!$id) {
			msg('错误的参数');
		}
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$title = _POST('title');
			$view_order = _POST('view_order', 0);
			$is_hide = _POST('is_hide', 0);
			$parent_id = _POST('parent_id', 0);
			$img = _POST('img');
			
			$insert = array('title'=>$title, 'view_order'=>$view_order, 'is_hide'=>$is_hide, 'parent_id'=>$parent_id);
			if ($img) {
				$insert['img'] = $img;
			}
			_model('category')->update(array('id'=>$id), $insert);
			redirect('?url=category&id='.$parent_id);
		} else {
			$info = _model('category')->read(array('id'=>$id));
			Smarty3::instance()->assign('info', $info);
			Smarty3::instance()->display('h_dev_html/category_create.html');
		}
	}
	
	function delete()
	{
		$id = _GET('id', 0);
		if (!$id || !$data = _model('category')->read(array('id'=>$id))) {
			msg('错误的参数');
		}
		if (get_category_type($id)) {
			$info = _model('category')->read(array('parent_id'=>$id));
			if ($info) {
				msg('存在下级无法删除');
			} else {
				_model('category')->delete(array('id'=>$id));
				redirect('?url=category&id='.$data['parent_id']);
			}
		} else {
			msg('该专辑下存在照片无法删除，必须删除该专辑下的所有照片才可删除该专辑');
		}
		
	}
}


?>